package com.smart.car.common.res.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.*;

/**
 * 腾讯云短信服务V3.0
 *
 * @author zoutao.blog.csdn.net
 * @date 2021/6/2
 */
public class SampleByTxUtil {

    //短信发送
    public static void main(String[] args) {
        String SecretId = "测试";
        String SecretKey = "测试";
        //sendSmsByTxy(SecretId, SecretKey);
        //sendStatusStatistics(SecretId, SecretKey);
        //callbackStatusStatistics(SecretId, SecretKey);
        PullSmsSendStatus(SecretId, SecretKey);
    }

    /**
     * 使用AK&AKS创建短信客户端SmsClient
     *
     * @param secretId
     * @param secretKey
     * @return SmsClient
     */
    public static SmsClient createClient(String secretId, String secretKey) {
        Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项，可选，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        // SDK会自动指定域名,可以不配置（服务商域名）
        httpProfile.setEndpoint("sms.tencentcloudapi.com");
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        //Endpoint (地域信息），可以直接填写字符串ap-guangzhou，或引用预设的常量
        SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
        return client;
    }

    /** 发送短信，可以写在impl也可以抽离到工具类*/
    public static String sendSmsByTxy(String secretId, String secretKey){
        try{
            SmsClient client = SampleByTxUtil.createClient(secretId, secretKey);

            SendSmsRequest req = new SendSmsRequest();
            //下发手机号码，采用 E.164 标准，格式为+[国家或地区码][手机号]，单次请求最多支持200个手机号
            String[] phoneNumberSet = {"+8618666666666"};
            req.setPhoneNumberSet(phoneNumberSet);
            //申请的材料
            req.setSmsSdkAppid("140666660");
            req.setSign("空空代码网");
            req.setTemplateID("986666");

            //模板参数
            String[] templateParamSet1 = {"9527", "15"};
            req.setTemplateParamSet(templateParamSet1);

            //发送短信请求
            SendSmsResponse resp = client.SendSms(req);
            System.out.println(SendSmsResponse.toJsonString(resp));
            return SendSmsResponse.toJsonString(resp);
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return null;
    }

    /** 拉取回执状态*/
    public static void PullSmsSendStatus(String SecretId,String SecretKey){
        try{
            SmsClient client = SampleByTxUtil.createClient(SecretId, SecretKey);
            PullSmsSendStatusRequest req = new PullSmsSendStatusRequest();
            req.setLimit(10L);  // 设置拉取最大条数，最多100条
            req.setSmsSdkAppid("14006666");

            PullSmsSendStatusResponse resp = client.PullSmsSendStatus(req);
            // 输出json格式的字符串回包
            System.out.println(PullSmsSendStatusResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }

    }

    /**
     * 发送短信的数量统计
     * SendStatusStatistics:{FeeCount:计费条数统计,RequestCount:提交量统计,RequestSuccessCount:提交成功量统计}
     * RequestId:唯一请求 ID，
     * {
     *   "Response": {
     *     "SendStatusStatistics": {
     *       "FeeCount": 0,
     *       "RequestCount": 1,
     *       "RequestSuccessCount": 0
     *     },
     *     "RequestId": "9e6981c8-db52-4720-abb9-ae14decfa25d"
     *   }
     * }
     * @author zoutao
     * @version v2.0
     * @date 2021/6/2
     */
    public static void sendStatusStatistics(String SecretId,String SecretKey){
        try{
            SmsClient client = SampleByTxUtil.createClient(SecretId, SecretKey);

            SendStatusStatisticsRequest req = new SendStatusStatisticsRequest();
            req.setStartDateTime(2021060213L); //开始日期yyyyMMdd格式
            req.setEndDataTime(2021060218L); //结束日期
            req.setSmsSdkAppid("1400666666"); //应用id
            req.setLimit(0L);           //上限和偏移量，目前为固定值0
            req.setOffset(0L);

            SendStatusStatisticsResponse resp = client.SendStatusStatistics(req);
            System.out.println(SendStatusStatisticsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }

    /**
     * 回执情况短信数据统计
     * CallbackCount		    短信回执量统计。
     * CallbackSuccessCount：    短信提交成功量统计。
     * CallbackFailCount		短信回执失败量统计。
     * CallbackSuccessCount		短信回执成功量统计。
     * InternalErrorCount		运营商内部错误统计。
     * InvalidNumberCount		号码无效或空号统计。
     * ShutdownErrorCount		停机、关机等错误统计。
     * BlackListCount		    号码拉入黑名单统计。
     * 结果：
     * {
     *   "Response": {
     *     "CallbackStatusStatistics": {
     *       "CallbackCount": 0,
     *       "RequestSuccessCount": 0,
     *       "CallbackFailCount": 0,
     *       "CallbackSuccessCount": 0,
     *       "InternalErrorCount": 0,
     *       "InvalidNumberCount": 0,
     *       "ShutdownErrorCount": 0,
     *       "BlackListCount": 0,
     *       "FrequencyLimitCount": 0
     *     },
     *     "RequestId": "d4c7ba0f-e80b-45cc-8161-fe5dc590d444"
     *   }
     * }
     *
     * @author zoutao
     * @version v2.0
     * @date 2021/6/2
     */
    public static void callbackStatusStatistics(String SecretId,String SecretKey){
        try{
            SmsClient client = SampleByTxUtil.createClient(SecretId, SecretKey);

            CallbackStatusStatisticsRequest req = new CallbackStatusStatisticsRequest();
            //开始时间，yyyymmddhh 需要拉取的起始时间，精确到小时
            req.setStartDateTime(2021060213L);
            /* 结束时间，yyyymmddhh 需要拉取的截止时间，精确到小时，注：EndTime 必须大于 beginTime */
            req.setEndDataTime(2021060218L);
            req.setSmsSdkAppid("14006666666");
            // 设置拉取最大条数，最多100条
            req.setLimit(5L);
            /* 偏移量 注：目前固定设置为0 */
            req.setOffset(0L);
            CallbackStatusStatisticsResponse resp = client.CallbackStatusStatistics(req);
            System.out.println(CallbackStatusStatisticsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }
}
